<?php
/**
 * TeacherAction 
 * 教师管理模块
 * 更新日志:
 *   2011-5-26:实现了基本的差删改查功能,但教师头像的上传还没实现
 */
class TeacherAction extends Action{
	/**
	 * __construct 
	 * 构造函数,加入登陆验证,防止非法登陆
	 * @access public
	 * @return void
	 */
	public function __construct(){
		parent::__construct();
		if(!isset($_SESSION['admin_id'])){
			header("Content-Type:text/html; charset=utf-8");
			$this->redirect('Index/login',null,3,"非法登录,3秒后为你跳转到登录页面..");
		}
	}
	/**
	 * index 
	 * 列表显示当前所有教师
	 * @access public
	 * @return void
	 */
	public function index(){
		$teacherModel = new Model('Teacher');
		$teachers = $teacherModel->select();
		$this->assign('teachers',$teachers);
    	$this->display();
    }
    /**
     * add 
     * 添加一个新教师
     * @access public
     * @return void
     */
    public function add(){
		if(isset($_POST['name'])){
			import('ORG.NET.UploadFile');
			$upload = new UploadFile();
			$upload->maxSize = 3145728;
			$upload->allowExts = array('jpg','gif','png','jpeg');
			$upload->savePath = 'Public/teacherAvatar/';
			if(!$upload->upload()){
				$this->error($upload->getErrorMsg());
			}else{
				$info = $upload->getUploadFileInfo();
			}
			$teacherModel = new Model('Teacher');
			$teacherModel->create();
			$teacherModel->avatar = $info[0]["savename"];
			if($teacherModel->add()){
				$this->redirect('index');
			}
		}
    	$this->display();
    }

	/**
	 * modify 
	 * 修改教师资料
	 * @access public
	 * @return void
	 */
	public function modify(){
		if(isset($_GET['teacher_id']) and preg_match('/\d+/',$_GET['teacher_id'])){
			$teacherModel = new Model('Teacher');
			$teacher=$teacherModel->where("teacher_id={$_GET['teacher_id']}")->find();
			if($teacher !== false){
				$this->assign('teacher',$teacher);
				$this->display();
				return;
			}
		}
		if(isset($_POST['name'])){
			import('ORG.NET.UploadFile');
			$upload = new UploadFile();
			$upload->maxSize = 3145728;
			$upload->allowExts = array('jpg','gif','png','jpeg');
			$upload->savePath = 'Public/teacherAvatar/';
			if(!$upload->upload()){
				$this->error($upload->getErrorMsg());
			}else{
				$info = $upload->getUploadFileInfo();
			}
			$teacherModel = new Model('Teacher');
			$teacherModel->create();
			$teacherModel->avatar = $info[0]["savename"];
			$teacherModel->save();
			$this->redirect('index');
		}
		header("Content-Type:text/html; charset=utf-8");
		$this->redirect('Index/login',null,3,"非法操作,3秒后为你跳转到登录页面..");
	}

	/**
	 * delete 
	 * 删除一个教师
	 * @access public
	 * @return void
	 */
	public function delete(){
		if(isset($_GET['teacher_id']) and preg_match('/\d+/',$_GET['teacher_id'])){
			$teacherModel = new Model('Teacher');
			$teacherModel->where("teacher_id={$_GET['teacher_id']}")->delete();
			$this->redirect('index');
		}
	}
}
?>
